package cn.iocoder.yudao.module.system.framework.sms.core.client.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.http.HttpUtil;
import cn.iocoder.yudao.framework.common.core.KeyValue;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsReceiveRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsSendRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.client.dto.SmsTemplateRespDTO;
import cn.iocoder.yudao.module.system.framework.sms.core.enums.SmsTemplateAuditStatusEnum;
import cn.iocoder.yudao.module.system.framework.sms.core.property.SmsChannelProperties;

import java.util.HashMap;
import java.util.List;

public class ZhihuiAnfangSmsClient  extends AbstractSmsClient{
    public ZhihuiAnfangSmsClient(SmsChannelProperties properties) {
        super(properties);
        Assert.notEmpty(properties.getApiKey(), "apiKey 不能为空");
        Assert.notEmpty(properties.getApiSecret(), "apiSecret 不能为空");
    }

    @Override
    public SmsSendRespDTO sendSms(Long logId, String mobile, String apiTemplateId, List<KeyValue<String, Object>> templateParams) throws Throwable {
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("userCode", properties.getApiKey());
        paramMap.put("userPass", properties.getApiSecret());
        paramMap.put("DesNo", mobile);
        // 确保templateParams不为空且有元素
        Assert.notEmpty(templateParams, "templateParams 不能为空");
        // 从templateParams获取code值
        String code = templateParams.get(0).getValue().toString();
        String content = String.format("您的验证码%s，该验证码 5 分钟内有效，请勿泄漏于他人！", code);
        paramMap.put("Msg", "【" + properties.getSignature() + "】" + content);
        String repCode = HttpUtil.post(properties.getCallbackUrl(), paramMap);
        SmsSendRespDTO smsSendRespDTO = new SmsSendRespDTO();
        return smsSendRespDTO.setSuccess(true).setSerialNo(repCode);

    }

    @Override
    public List<SmsReceiveRespDTO> parseSmsReceiveStatus(String text) throws Throwable {
        return List.of();
    }

    @Override
    public SmsTemplateRespDTO getSmsTemplate(String apiTemplateId) throws Throwable {

        return new SmsTemplateRespDTO().setId(apiTemplateId).setContent("")
                .setAuditStatus(SmsTemplateAuditStatusEnum.SUCCESS.getStatus()).setAuditReason("");
    }
}
